package com.cskaoyan.connectionpool.opensource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * 创建日期: 2022/07/01 16:17
 *
 * @author ciggar
 */
public class C3p0Utils {

    static ComboPooledDataSource comboPooledDataSource;

    static {

        // c3p0数据库连接池
        comboPooledDataSource = new ComboPooledDataSource("mysql-config");

//        comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/42th_rev?useSSL=false&characterEncoding=utf8");
//        comboPooledDataSource.setUser("root");
//        comboPooledDataSource.setPassword("123456");
//        try {
//            comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
//        } catch (PropertyVetoException e) {
//            e.printStackTrace();
//        }

    }


    // 获取连接的方法
    public static Connection getConnection(){

        Connection connection = null;
        try {
            connection = comboPooledDataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;

    }
}
